package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeService implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public int deleteByPrimaryKey(Long id) {


        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Employee record) {
        if (record.getPassword()!=null){
            Md5Hash md5Hash = new Md5Hash(record.getPassword(),record.getName());
            record.setPassword(md5Hash.toString());
        }

        return employeeMapper.insert(record);
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Employee record) {
        return employeeMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<Employee> list(EmployeeQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "id asc");
        List<Employee> list = employeeMapper.selectForList(qo);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public void batchDeleteByEmployeeIds(Long[] ids) {
        employeeMapper.batchDeleteByEmployeeIds(ids);
    }

    @Override
    public Employee selectByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

    //导出XLS
    @Override
    public Workbook getWorkbook() {
        //创建一个XLS表格
        Workbook workbook = new HSSFWorkbook();
        //创建工作区
        Sheet sheet = workbook.createSheet("employee");
        Row headrow = sheet.createRow(0);
        headrow.createCell(0).setCellValue("编号");
        headrow.createCell(1).setCellValue("姓名");
        headrow.createCell(2).setCellValue("年龄");
        //获取要导出的数据
        List<Employee> employees = employeeMapper.selectAll();
        //循环写入表格
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            Row row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getId());
            row.createCell(1).setCellValue(employee.getUsername());
            row.createCell(2).setCellValue(employee.getAge());
        }
        return workbook;
    }

    //导入功能 模板下载
    @Override
    public Workbook getWorkbook_model() {
        //创建一个XLS表格
        Workbook workbook = new HSSFWorkbook();
        //创建工作区
        Sheet sheet = workbook.createSheet("employee");
        Row headrow = sheet.createRow(0);
        //headrow.createCell(0).setCellValue("编号");
        headrow.createCell(0).setCellValue("姓名");
        headrow.createCell(1).setCellValue("年龄");

        return workbook;
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //模板的解析
        //读取模板内容
        //将数据保存到数据库
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        //循环去解析模板内容
        int maxline = sheet.getLastRowNum();
        for (int i=1;i<=maxline;i++){
            Employee employee = new Employee();
            Row row = sheet.getRow(i);

            //double cell0 = row.getCell(0).getNumericCellValue();
           //employee.setId((long) cell0);

            if (row.getCell(0) .getCellType() == CellType.NUMERIC){
                //用户名是纯数字 但数据库中username字段是varchar类型 需要使用double先接收  double转成string
                double cell0 = row.getCell(0).getNumericCellValue();
                //使用   "" + cell0
                employee.setUsername(""+cell0);
            }else {
                //用户名不是纯数字
                String cell0 = row.getCell(0).getStringCellValue();
                employee.setUsername(cell0);
            }

            double cell1 = row.getCell(1).getNumericCellValue();
            employee.setAge((int) cell1);

            insert(employee);

        }
    }
}
